package com.bookcode;


import com.bookcode.dao.UserRepository;
import com.bookcode.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication  {
    private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);    }
    @Bean
    public CommandLineRunner demo(UserRepository repository) {
        return (args) -> {
            //存储5条用户记录到数据库
            repository.save(new User("Jack", "Bauer"));
            repository.save(new User("Chloe", "O'Brian"));
            repository.save(new User("Kim", "Bauer"));
            repository.save(new User("David", "Palmer"));
            repository.save(new User("Michelle", "Dessler"));
            // 将所有user信息以日志形式输出到控制台
            log.info("Users found with findAll():");
            log.info("-------------------------------");
            for (Object user : repository.findAll()) {
                log.info(user.toString());            }
            // 获取id=1的user信息，并以日志形式在控制台输出
            repository.findById(1L)
                    .ifPresent(User -> {
                        log.info("User found with findById(1L):");
                        log.info("--------------------------------");
                        log.info(User.toString());
                    });
        };
    }
}
